/*-----------------------------------------------------------------------*/
/* Low level disk I/O module skeleton for FatFs     (C)ChaN, 2019        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control modules to the FatFs module with a defined API.       */
/*-----------------------------------------------------------------------*/

#include "ff.h"			/* Obtains integer types */
#include "diskio.h"		/* Declarations of disk functions */

#include "disk.h"
///* Definitions of physical drive number for each drive */
//#define DEV_RAM		0	/* Example: Map Ramdisk to physical drive 0 */
//#define DEV_MMC		1	/* Example: Map MMC/SD card to physical drive 1 */
//#define DEV_USB		2	/* Example: Map USB MSD to physical drive 2 */


/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{
	DSTATUS stat = STA_NOINIT;
	switch (pdrv) {
	case DEV_RAM :
//		stat = RAM_disk_status();
//        break;
//
//	case DEV_MMC :
		stat = MMC_disk_status();
        break;

	case DEV_USB :
		stat = USB_disk_status();
        break;
	}
	return stat;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
	BYTE pdrv				/* Physical drive nmuber to identify the drive */
)
{
	DSTATUS stat = STA_NOINIT;
	switch (pdrv) {
	case DEV_RAM :
//		stat = RAM_disk_initialize();
//        break;
//
//	case DEV_MMC :
		stat = MMC_disk_initialize();
        break;

	case DEV_USB :
		stat = USB_disk_initialize();
        break;
	}
	return stat;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
	BYTE *buff,		/* Data buffer to store read data */
	LBA_t sector,	/* Start sector in LBA */
	UINT count		/* Number of sectors to read */
)
{
	DSTATUS stat = STA_NOINIT;
	switch (pdrv) {
	case DEV_RAM :
//		stat = RAM_disk_read(buff, sector, count);
//        break;
//
//	case DEV_MMC :
		stat = MMC_disk_read(buff, sector, count);
        break;

	case DEV_USB :
		stat = USB_disk_read(buff, sector, count);
        break;
	}
	return stat;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if FF_FS_READONLY == 0

DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber to identify the drive */
	const BYTE *buff,	/* Data to be written */
	LBA_t sector,		/* Start sector in LBA */
	UINT count			/* Number of sectors to write */
)
{
	DSTATUS stat = STA_NOINIT;
	switch (pdrv) {
	case DEV_RAM :
//		stat = RAM_disk_write(buff, sector, count);
//        break;
//
//	case DEV_MMC :
		stat = MMC_disk_write(buff, sector, count);
        break;

	case DEV_USB :
		stat = USB_disk_write(buff, sector, count);
        break;
	}

	return stat;
}

#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	DRESULT stat  = STA_NOINIT;
	switch (pdrv) {
	case DEV_RAM :
		// Process of the command for the RAM drive
//		stat = RAM_disk_ioctl(cmd, buff);
//		break;
//
//	case DEV_MMC :
		// Process of the command for the MMC/SD card
		stat = MMC_disk_ioctl(cmd, buff);
		break;

	case DEV_USB :
		// Process of the command the USB drive
		stat = USB_disk_ioctl(cmd, buff);
		break;
	}

	return stat;
}


/*-----------------------------------------------------------------------*/
/* User defined function to give a current time to fatfs module          */
/* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
/* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
DWORD get_fattime (void)
{
#if  GET_FATTIME_EN
    DWORD temp;
    //  rtc_get();
    //temp=(timer.w_year-1980)<<25|timer.w_month<<21|timer.w_date<<16|timer.hour<<11|timer.min<<5|timer.sec;
    return temp;
#else
    return 0;
#endif
}
